In [2]:
%matplotlib inline

import os
from PyFin.api import *
from alphamind.api import *

1. 公式书写



In [33]:
universe_name = 'zz1000'
formula = CSTopN(LAST('ROE'), 3, groups='sw1')
ref_date = '2019-02-14'

2. 获取数据



In [35]:
depends = formula.fields
engine = SqlEngine(os.environ['DB_URI'])
universe = Universe(universe_name)
codes = universe.query(engine, dates=[ref_date])

In [43]:
factors = engine.fetch_factor(ref_date, depends, codes.code.tolist()).dropna()
factors.index = [1] * len(factors)
factors = factors[['code'] + depends]

3. Stock Screening



In [44]:
res = formula.transform(factors, name='factor', category_field='code')

In [45]:
res[res.factor == 1]


Out[45]:
factor code
1 1.0 16
1 1.0 34
1 1.0 43
1 1.0 55
1 1.0 429
1 1.0 516
1 1.0 520
1 1.0 639
1 1.0 672
1 1.0 789
1 1.0 881
1 1.0 885
1 1.0 892
1 1.0 918
1 1.0 968
1 1.0 2016
1 1.0 2033
1 1.0 2035
1 1.0 2061
1 1.0 2071
1 1.0 2168
1 1.0 2182
1 1.0 2234
1 1.0 2327
1 1.0 2343
1 1.0 2396
1 1.0 2518
1 1.0 2597
1 1.0 2626
1 1.0 2677
... ... ...
1 1.0 600231
1 1.0 600382
1 1.0 600569
1 1.0 600581
1 1.0 600596
1 1.0 600677
1 1.0 600681
1 1.0 600740
1 1.0 600763
1 1.0 600803
1 1.0 600828
1 1.0 603043
1 1.0 603055
1 1.0 603113
1 1.0 603165
1 1.0 603180
1 1.0 603305
1 1.0 603323
1 1.0 603399
1 1.0 603517
1 1.0 603587
1 1.0 603595
1 1.0 603596
1 1.0 603609
1 1.0 603660
1 1.0 603666
1 1.0 603708
1 1.0 603730
1 1.0 603773
1 1.0 603848

82 rows × 2 columns


In [ ]: